/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package systole.persistence.brokersDB;

import java.util.List;
import org.hibernate.Criteria;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import systole.domain.clinicalInformation.Sport;
import systole.exceptions.ExceptionDAO;
import systole.persistence.FacadeDB;
import systole.persistence.brokersInterface.SportSyncBroker;
import systole.synchronization.remote.entities.SportRemote;

/**
 *
 * @author Juan Manuel
 */
public class SportSyncBrokerDB extends BrokerDB implements SportSyncBroker {

    public void saveSportRemote(SportRemote sportRemote) throws ExceptionDAO {
        try {
            this.logger.logDebug("saving remote sport");
            Session currentSession = FacadeDB.getInstance().getCurrentSession();
            currentSession.save(sportRemote);
            this.logger.logDebug("remote sport saved");
        } catch (HibernateException e) {
            this.logger.logError("error on save sport remote, msg: " + e.getMessage());
            throw new ExceptionDAO("No se pudieron guardar los cambios", e.fillInStackTrace());
        }
    }

    public List<SportRemote> getSportsToUpdate() throws ExceptionDAO {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    public List<Sport> getSportsToUpload() throws ExceptionDAO {
        try {
            this.logger.logDebug("getting sport to upload");
            Session currentSession = FacadeDB.getInstance().getCurrentSession();
            String hql = "select s from SportRemote sr right join sr.sport s where sr=null";
            Query query = currentSession.createQuery(hql);
            @SuppressWarnings("unchecked")
            List<Sport> list = query.list();
            this.logger.logDebug("get successfully");
            return list;
        } catch (HibernateException e) {
            this.logger.logError("error on get sport to upload, msg: " + e.getMessage());
            throw new ExceptionDAO("No se pudieron obtener las actividades físicas", e.fillInStackTrace());
        }
    }

    public Sport getSportByRemoteId(int remoteId) throws ExceptionDAO {
        try {
            this.logger.logDebug("getting sport by remote id");
            Session currentSession = FacadeDB.getInstance().getCurrentSession();
            Criteria criteria = currentSession.createCriteria(
                    SportRemote.class).add(Restrictions.eq("remoteId", remoteId)).setMaxResults(1);
            SportRemote sportRemote = (SportRemote) criteria.uniqueResult();
            this.logger.logDebug("get successfully");
            return (sportRemote != null ? sportRemote.getSport() : null);
        } catch (HibernateException e) {
            this.logger.logError("error on get sport by remote id, msg: " + e.getMessage());
            throw new ExceptionDAO("No se pudieron obtener las actividades físicas", e.fillInStackTrace());
        }
    }

    public SportRemote getSportRemoteFromSport(Sport sport) throws ExceptionDAO {
        try {
            this.logger.logDebug("getting sport remote by local sport");
            Session currentSession = FacadeDB.getInstance().getCurrentSession();
            Criteria criteria = currentSession.createCriteria(
                    SportRemote.class).add(Restrictions.eq("sport", sport)).setMaxResults(1);
            SportRemote sportRemote = (SportRemote) criteria.uniqueResult();
            this.logger.logDebug("get successfully");
            return sportRemote;
        } catch (HibernateException e) {
            this.logger.logError("error on get remote sport by sport, msg: " + e.getMessage());
            throw new ExceptionDAO("No se pudieron obtener las actividades físicas", e.fillInStackTrace());
        }
    }

    public int getLastRemoteSportIdSynchronized() throws ExceptionDAO {
        try {
            this.logger.logDebug("getting last remote sport sync");
            Session currentSession = FacadeDB.getInstance().getCurrentSession();
            Criteria criteria = currentSession.createCriteria(
                    SportRemote.class).addOrder(Order.desc("remoteId")).setMaxResults(1);
            SportRemote sportRemote = (SportRemote) criteria.uniqueResult();
            this.logger.logDebug("get successfully");
            return (sportRemote != null ? sportRemote.getRemoteId() : -1);
        } catch (HibernateException e) {
            this.logger.logError("error on get last remote sport synchronized, msg: " + e.getMessage());
            throw new ExceptionDAO("No se pudieron obtener las actividades físicas", e.fillInStackTrace());
        }
    }
}
